package com.jiyongjun.component.core.impl;

import com.jiyongjun.component.config.LogSwitch;
import com.jiyongjun.component.core.model.MqLogContext;
import com.jiyongjun.component.utils.LogContextUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;

/**
 * <h2></h2>
 * <h3>描述：</h3>
 *
 * @author yongjun.jyj
 * @since 2022/7/14 20:21
 */
@Aspect
@Order
public class MqOnsWatchDogAspect {
    // @Around(value = "execution(public * com.aliyun.openservices.ons.api.Producer.*(..)) " +
    //         "&& args(message, ..)")
    // public Object aroundSend(ProceedingJoinPoint pjp, Message message) throws Throwable {
    //     MethodSignature signature = (MethodSignature) pjp.getSignature();
    //
    //     MqLogContext logContext = MqLogContext.getLogContext(LogContextUtils.generateMethodUniqueName(signature.getMethod()))
    //             .setType("send")
    //             .setApiName(LogContextUtils.generateMethodSimpleName(signature.getMethod()))
    //             .setMethod(signature.getMethod());
    //     try {
    //         Object o = pjp.proceed();
    //         if (o instanceof SendResult) {
    //             message.setMsgID(((SendResult) o).getMessageId());
    //         }
    //         return o;
    //     } catch (Throwable e) {
    //         logContext.setException(e);
    //         throw e;
    //     } finally {
    //         logContext.setMessage(message);
    //         logContext.close();
    //     }
    //
    // }
    //
    // @Around(value = "@within(subscription) " +
    //         "&& execution(public * com.aliyun.openservices.ons.api.MessageListener.consume(..)) " +
    //         "&& args(message, consumeContext)", argNames = "pjp,subscription,message,consumeContext")
    // public Object aroundReceive(ProceedingJoinPoint pjp, Subscription subscription, Message message, ConsumeContext consumeContext) throws Throwable {
    //     MethodSignature signature = (MethodSignature) pjp.getSignature();
    //
    //     String methodUniqueName = LogContextUtils.generateMethodUniqueName(signature.getMethod());
    //     MqLogContext logContext = MqLogContext.getLogContext(methodUniqueName)
    //             .setExpression(subscription.expression())
    //             .setMessage(message)
    //             .setType("receive")
    //             .setApiName(LogContextUtils.generateMethodSimpleName(signature.getMethod()))
    //             .setMethod(signature.getMethod());
    //
    //     try {
    //         // 2.执行目标方法
    //         Object result = pjp.proceed();
    //
    //         if (!LogSwitch.LOG_RESULT_API_BLACKLIST.contains(logContext.getApiUniqueName())) {
    //             logContext.setResult(result);
    //         }
    //
    //         return result;
    //     } catch (Throwable throwable) {
    //         logContext.setException(throwable);
    //         throw throwable;
    //     } finally {
    //         logContext.close();
    //     }
    // }
}
